﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _404park.ApplicationServices.Crawler.Parser
{
    public class FeedParserManager
    {
        private static Dictionary<string, IFeedParser> _parsers;
        private static FeedParserFactory _factory;

        public FeedParserManager()
            : this(null)
        {
        }

        public FeedParserManager(FeedParserFactory factory)
        {
            _parsers = new Dictionary<string, IFeedParser>();
            _factory = factory ?? new FeedParserFactory();
        }

        public IFeedParser GetParser(string provider)
        {
            IFeedParser parser;
            _parsers.TryGetValue(provider, out parser);

            if (parser == null)
            {
                parser = _factory.GetParser(provider);
                if (parser != null)
                    _parsers.Add(provider, parser);
            }

            return parser;
        }
    }
}
